<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/16
 * Time: 14:13
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\AnchorService;
use App\Constant\ErrorConstant;

class AnchorController extends BaseController
{
    protected $service;

    public function __construct(Request $request, AnchorService $service)
    {
        parent::__construct($request);

        $this->service = $service;
    }

    /**
     * 主播列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listAnchor()
    {
        $limit  =   $this->request->input('page_size', NULL);
        $page   =   $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->anchorList($limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_LIST_ERROR, $result);
        }
    }

    /**
     * 主播添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addAnchor()
    {
        $validator  =   Validator::make($this->request->post(), [
            'nick_name' =>  'required|string',
            'head_icon' =>  'string'
        ], [
            'nick_name.required'    =>  '主播昵称不能为空',
            'nick_name.string'      =>  '主播昵称必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->anchorAdd($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_ADD_ERROR, $result);
        }
    }

    /**
     * 主播删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteAnchor()
    {
        $validator    =   Validator::make($this->request->post(), [
            'anchor_id' =>  'required|integer|exists:mini_anchor,id'
        ], [
            'anchor_id.required'    =>  '主播ID不能为空',
            'anchor_id.integer'     =>  '主播ID必须为整型',
            'anchor_id.exists'      =>  '主播不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_DELETE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->anchorDelete((int) $this->request->post('anchor_id'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_DELETE_ERROR, $result);
        }
    }

    /**
     * 主播修改
     * @return \Illuminate\Http\JsonResponse
     */
    public function editAnchor()
    {
        $validator    =   Validator::make($this->request->post(), [
            'anchor_id' =>  'required|integer|exists:mini_anchor,id',
            'nick_name' =>  'required|string',
            'head_icon' =>  'string'
        ], [
            'anchor_id.required'    =>  '主播ID不能为空',
            'anchor_id.integer'     =>  '主播ID必须为整型',
            'anchor_id.exists'      =>  '主播不存在',
            'nick_name.required'    =>  '主播昵称不能为空',
            'nick_name.string'      =>  '主播昵称必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_EDIT_ERROR, $validator->errors()->first());
        }

        $anchor_id  =   (int) $this->request->post('anchor_id');
        [$flag, $result]    =   $this->service->anchorEdit($this->request->post(), $anchor_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ANCHOR_EDIT_ERROR, $result);
        }
    }
}
